package com.lry.crawlers.listener;

import com.lry.crawlers.config.RabbitMQConfig;
import com.lry.crawlers.cartoon.entity.CartoonVariable;
import com.lry.crawlers.cartoon.pipeline.CartoonPipeline;
import com.lry.crawlers.cartoon.processor.CartoonProcessor;
import com.lry.crawlers.cartoon.service.CartoonService;
import com.lry.crawlers.cartoon.service.CartoonVariableService;
import com.lry.crawlers.information.entity.Information;
import com.lry.crawlers.information.service.InformationService;
import com.lry.crawlers.message.service.MessageService;
import com.lry.crawlers.util.ProcessVariable;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

import java.util.Date;

@Component
public class CartoonMQListener {
    @Autowired
    CartoonPipeline cartoonPipeline;
    @Autowired
    CartoonVariableService cartoonVariableService;
    @Autowired
    CartoonService cartoonService;
    @Autowired
    InformationService informationService;
    @Autowired
    MessageService messageService;



    //监听cartoon_queue队列
    @RabbitListener(queues = RabbitMQConfig.CARTOONEUE_NAME)
    public void ListenerCartoonQueue(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        String name = new String(message.getBody());

            String[] split = name.split(",");
            Thread.sleep(3000);
            int i= informationService.updateInformation(new Information(split[1],"漫画","采集关键词："+split[0],"进行中",new Date(),null));
            try {
            cartoonVariableService.update();
            //搜索内容
            for (CartoonVariable f : ProcessVariable.cVariable.values()) {
                Spider.create(new CartoonProcessor(f, cartoonService))
                        //设置爬取数据的地址
                        .addUrl(f.getSearchUrl()+split[0])
                        //setUUID只有在分布式的时候是必须的
                        .setUUID("1234567")
                        .addPipeline(cartoonPipeline)
                        //设置redisScheduler
                        //.setScheduler(new RedisScheduler(jedisPool))
                        //设置多线程
                        .thread(10)
                        //执行爬虫
                        .run();
            }
            channel.basicAck(deliveryTag, false);
            informationService.updateInformation(new Information(split[1],"漫画","采集关键词："+split[0],"完成",null,new Date()));

        } catch (Exception e) {
            channel.basicNack(deliveryTag, false, false);
            informationService.updateInformation(new Information(split[1],"漫画","采集关键词："+split[0],"错误",null,new Date()));
            messageService.setMessage(new com.lry.crawlers.message.entity.Message("漫画采集出现错误！title："+split[0]+"异常："+e,new Date()));
        }}

    }






